﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using BIS.Portal.Core.Base;
using BIS.Portal.Core.Global;
using BIS.Portal.Modules.CMS.BO;
using BIS.Portal.Modules.CMS.Entity;
using BIS.Utility.Converter;


namespace BIS.Portal.Modules.CMS
{
    public partial class CmsListOtherArticleExcludeList : ControlBase
    {
        #region Load Settings
        public string SLTinHienThi
        {
            get { return GetControlSetting<string>(ModuleSettingNames.CmsListOtherArticleExcludeList.txtSLTin, PortalContext.LanguageAlias); }
        }
        public bool HienThiTieuDe
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsListOtherArticleExcludeList.ddlTieuDe, PortalContext.LanguageAlias); }
        }
        public bool HienThiAnh
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsListOtherArticleExcludeList.ddlHinhAnh, PortalContext.LanguageAlias); }
        }
        public bool HienThiMoTa
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsListOtherArticleExcludeList.ddlHienThiMoTa, PortalContext.LanguageAlias); }
        }
        public bool HienThiLinkXemCT
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsListOtherArticleExcludeList.ddlLinkXemCT, PortalContext.LanguageAlias); }
        }

        #endregion

        private int _page_index;
        private string _category_name;
        private int _cate_id;
        protected void Page_Load(object sender, EventArgs e)
        {
            loadData();
        }
        private void loadData()
        {
            Nhan_Tieu_De.Text = GetLanguageValue("LableTitle");
            if (Request[ModuleQueryStringKey.CategoryId] != null)
            {
                _cate_id = Functions.GetInt(Request[ModuleQueryStringKey.CategoryId]);
                if (Request[ModuleQueryStringKey.PageIndex] != null)
                {
                    _page_index = Functions.GetInt(Request[ModuleQueryStringKey.PageIndex]);
                }
                else
                {
                    _page_index = 1;
                }
                IList<ClientCmsArticleAndCategoryEntity> list = ClientCmsArticleBO.GetArticleByCategoryId(
                    _cate_id, PortalContext.LanguageInUseId, _page_index, Functions.GetInt(SLTinHienThi));
                _category_name = ClientCmsArticleBO.CategoryName;
                Tin_Tuc.DataSource = list;
                Tin_Tuc.DataBind();
            }
        }

        protected void Tin_Tuc_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ClientCmsArticleAndCategoryEntity objArticle = (ClientCmsArticleAndCategoryEntity)e.Item.DataItem;
                if (null != objArticle)
                {
                    #region Url Chi Tiết

                    string urlChiTiet = WebHelpers.GetLink(ModuleQueryStringKey.UrlLinkChiTiet, objArticle.Page_alias,
                                                           new string[]
                                                               {
                                                                   ModuleQueryStringKey.CategoryId,
                                                                   ModuleQueryStringKey.ArticleId,
                                                                   ModuleQueryStringKey.CategoryName,
                                                                   ModuleQueryStringKey.ArticleTile
                                                               },
                                                           new string[]
                                                               {
                                                                   Convert.ToString(objArticle.Category_id),
                                                                   Convert.ToString(objArticle.Article_id),
                                                                   Functions.UnicodeToKoDauAndGach(_category_name),
                                                                   Functions.UnicodeToKoDauAndGach(objArticle.Article_title)
                                                               }
                        );

                    #endregion

                    #region Bind data Image
                    if (HienThiAnh)
                    {
                        Image imgAnh = e.Item.FindControl("Anh_Minh_Hoa") as Image;
                        if (imgAnh != null)
                        {
                            imgAnh.ImageUrl = objArticle.Thumb_image;
                            imgAnh.AlternateText = objArticle.Article_title;
                            imgAnh.Attributes.Add("onerror", "ImageNotFound(this);");
                        }
                    }
                    #endregion
                    #region Bind data Link Tiêu đề
                    HyperLink lnkTieuDe = e.Item.FindControl("Tieu_De") as HyperLink;
                    if (lnkTieuDe != null)
                    {
                        lnkTieuDe.ToolTip = objArticle.Article_title;
                        lnkTieuDe.NavigateUrl = urlChiTiet;
                        lnkTieuDe.Text = objArticle.Article_title;
                    }
                    #endregion
                    #region Bind Nội dung trong Literal
                    if (HienThiMoTa)
                    {
                        Literal ltrNoiDung = e.Item.FindControl("Noi_Dung") as Literal;
                        if (ltrNoiDung != null) ltrNoiDung.Text = objArticle.Article_description;
                    }
                    #endregion
                    #region Bind data Link Xem tiếp

                    if (HienThiLinkXemCT)
                    {
                        HyperLink lnkXemTiep = e.Item.FindControl("Link_Xem_Tiep") as HyperLink;
                        if (lnkXemTiep != null)
                        {
                            lnkXemTiep.Text = GetLanguageValue("ReadMoreButton");
                            lnkXemTiep.NavigateUrl = urlChiTiet;
                        }
                    }

                    #endregion
                }
            }
        }
    }
}